package com.mxin.ui.base

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.LifecycleOwner
import com.max.baselib.widget.toolbar.ToolbarConstants
import com.max.baselib.widget.toolbar.ToolbarHelper
import com.mxin.ui.global.GlobalViewManager
import com.mxin.ui.loading.IConfigLoadingView
import com.mxin.ui.loading.LoadingViewHelper

abstract class BaseActivity :AppCompatActivity(), IBaseView, IConfigLoadingView{

    private var titleView: View?=null
    private var loadingDialog: Dialog ?= null
    private var loadingViewHelper: LoadingViewHelper ?= null

    /**
     * 初始化环境
     */
    fun initEnvironment(){}

    //设置内容布局
    abstract fun setContentView()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initEnvironment()
        setContentView()
        initView()
        initData()
    }

    override fun getContext(): Context {
        return this
    }

    override fun getLifeCycleOwner(): LifecycleOwner {
        return this
    }

    override fun finishActivity() {
        finish()
    }

    override fun onDestroy() {
        dismissLoadingView()
        loadingDialog=null
        loadingViewHelper = null
        super.onDestroy()
    }

    open fun backPrevious(){
        finish()
    }

    //<editor-fold desc="配置titleView"
    protected fun setTitleView(view:View,
                             title:String,
                             titleStyle:Int = ToolbarConstants.def_titleStyle_NaN,
                             bgColor:Int = ToolbarConstants.def_bgColor_NaN,
                             showNavigation:Boolean = true,
                             navigationIcon:Int = ToolbarConstants.def_back_navigationResId,
                             menuResId:Int = -1
    ){
        titleView = ToolbarHelper.from<Toolbar>(view)
            {
                it.title = title
                it.titleStyle = titleStyle
                it.bgColor = bgColor
                it.showNavigation = showNavigation
                it.navigationIcon = navigationIcon
                it.menuResId = menuResId
                it.navigationClickListener = View.OnClickListener { backPrevious() }
            }
    }

    fun getTitleView(): View?{
        return titleView
    }

    //</editor-fold>


    //<editor-fold desc="加载Dialog">

    override fun showLoadingView(init:((Dialog)->Unit)?) {
        if(loadingDialog==null){
            loadingDialog = GlobalViewManager.createLoadingView(this)
        }
        init?.invoke(loadingDialog!!)
        loadingDialog!!.show()
    }

    override fun dismissLoadingView() {
        loadingDialog?.dismiss()
    }

    //</editor-fold>


    //<editor-fold desc="初始化加载动画View" >

    private fun getLoadingViewHelper():LoadingViewHelper{
        if(loadingViewHelper==null){
            loadingViewHelper = LoadingViewHelper.bindView(this)
        }
        return loadingViewHelper!!
    }

    override fun showLoading() {
        getLoadingViewHelper().showLoading()
    }

    override fun showLoadSuccess() {
        getLoadingViewHelper().showLoadSuccess()
    }

    override fun showLoadFailed() {
        getLoadingViewHelper().showLoadFailed()
    }

    override fun showEmpty() {
        getLoadingViewHelper().showEmpty()
    }

    override fun isShowLoading(): Boolean {
        return loadingViewHelper?.isShowLoading()?:false
    }

    //</editor-fold>


}